
<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>报表权限设置</title>

    <link rel="stylesheet" href="/webroot/decision/resfile?path=/com/fr/plugin/provider/entrance/css/layui.css"  >



    <link rel="stylesheet" href="/webroot/decision/resfile?path=/com/fr/plugin/provider/entrance/css/modules/layer/default/layer.css">

    <script src="/webroot/decision/resfile?path=/com/fr/plugin/provider/entrance/layui.js" charset="utf-8"></script>
    <!--    <script src="/webroot/decision/resfile?path=/com/fr/plugin/provider/entrance/modules/jquery.js" charset="utf-8"></script>-->

    <script src="/webroot/decision/resfile?path=/com/fr/plugin/provider/entrance/jquery.js"></script>
    <script src="/webroot/decision/resfile?path=/com/fr/plugin/provider/entrance/xm-sel.js"></script>

</head>

<html>


<body>
<div style="margin: 0 auto;width: 90%">

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend>报表权限设置</legend>
    </fieldset>

    <div class="layui-inline">
        <label class="layui-form-label"></label>
        <button  class="layui-btn" lay-submit="" lay-filter="query" onclick="query()">刷新</button>

        <button  class="layui-btn" lay-submit="" lay-filter="add" onclick="add()">新增</button>
    </div>

    <table class="layui-hide" id="fileAuthList" lay-filter="tableFilter"></table>

</div>
</body>




<script>

    <!--查询用户文件列表-->
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#fileAuthList'
            ,url:'/webroot/decision/vcs/auth/query/vcs'
            ,method:"post"
            ,limit:10
            ,height:500
            ,contentType:"application/json"
            ,page: true
            ,cols: [[
                {field:'fileId', width:"20%", title: "ID"}
                ,{field:'fileName', width:"15%", title: '文件名'}
                ,{field:'realName', width:"25%", title:"用户限制"}
                ,{field:'ipLimit', width:"25%", title:"IP限制"}
                ,{title: "操作",width: "15%",templet:optTool}
            ]]
            ,response: {
                statusCode: 200
            }
            ,parseData: function(res) {
                return {
                    "code": res.errorCode,
                    "msg": res.message,
                    "count": res.data.count,
                    "data": res.data.data
                }
            }
        });
    });



    <!--新增操作按钮-->
    function optTool(data) {
        return  "<button class=\"layui-btn layui-btn-sm  layui-btn-normal\" lay-event=\"update\" >修改</button>"
            + "<button class=\"layui-btn layui-btn-sm  layui-btn-danger\" lay-event=\"delete\" >删除</button>";
    }


    <!--查询列表-->
    function query(){
        layui.table.reload("fileAuthList",{
            page:{
                curr:1
            }
        })
    }



    layui.use('table', function() {
        var table = layui.table;
        //监听工具条
        table.on('tool(tableFilter)', function (obj) {
            if(obj.event === "update"){
                update(obj);
            }else if(obj.event === "delete"){
                deleteByAuthId(obj);
            }
        });
    });


    var UserSelectObj = function (name,val,isSelect,disabled){
        this.name = name;
        this.value = val;
        this.selected =isSelect;
        this.disabled = disabled;
    }

    function update(obj){
        clearUpdateDiv();
        var userList;
        $.ajax({
            url:"/webroot/decision/vcs/auth/query/user",
            type:"post",
            async:false,
            dataType:"json",
            data:'{}',
            contentType:"application/json",
            success:function(userRet){
                userList = userRet.data;
            }
        });
        if(userList == undefined || userList.length == 0){
            layer.alert('查询失败', {icon: 5});
            return;
        }
        console.info(obj.data);
        //初始化表单数据
        $("#updateFileName").val(obj.data.fileName);
        $("#updateShowFileName").val(obj.data.fileName);
        $("#updateIpLimit").val(obj.data.ipLimit);

        //初始化用户列表
        var defineUserArr = new Array();
        var selectUserNameArr = obj.data.userLimit.split(",");
        for(var i=0; i<userList.length; i++){
            var isSelect = selectUserNameArr.indexOf(userList[i].userName) > -1;
            defineUserArr[i] = new UserSelectObj(userList[i].realName,userList[i].userName,isSelect,false);
        }
        xmSelect.render({
            tips: '请选择用户',
            el: '#updateUserLimit',
            toolbar:{
                show: true,
            },

            name:"userNameList",
            data:defineUserArr
        })

        $("#updateDiv").css("display","block");
        layui.use("form",function (){
            layui.form.render('select',"updateForm")
        });
        layer.open({
            type: 1,
            shadeClose:false,
            title:"权限设置",
            skin: 'layui-layer-rim', //加上边框
            area: ['70%', '60%'], //宽高
            content:layui.$("#updateDiv"),
            end:function (){
                clearUpdateDiv();
            }
        });
    }



    function add(){
        clearAddDiv();
        var userList;
        var fileList;
        $.ajax({
            url:"/webroot/decision/vcs/auth/query/user",
            type:"post",
            async:false,
            dataType:"json",
            data:'{}',
            contentType:"application/json",
            success:function(userRet){
                userList = userRet.data;
            }
        });
        if(userList == undefined || userList.length == 0){
            layer.alert('查询失败', {icon: 5});
            return;
        }
        $.ajax({
            url:"/webroot/decision/vcs/auth/query/all/file",
            type:"post",
            async:false,
            dataType:"json",
            contentType:"application/json",
            success:function(fileRet){
                if(fileRet.errorCode == 200 ){
                    fileList =  fileRet.data;
                }
            }
        });
        if(fileList == undefined || fileList.length == 0){
            layer.alert('查询失败', {icon: 5});
            return;
        }

        //初始化表单数据
        for(var k=0; k<fileList.length; k++){
            $("#addFileList").append("<option value='"+fileList[k].fileName+"'>"+ fileList[k].fileName+"</option>")
        }
        var defineUserArr = new Array();
        for(var i=0; i<userList.length; i++){
            defineUserArr[i] = new UserSelectObj(userList[i].realName,userList[i].userName,false,false);
        }
        $("#addDiv").css("display","block");
        layui.use("form",function (){
            layui.form.render('select',"addForm")
        });
        xmSelect.render({
            tips: '请选择用户',
            el: '#addUserLimit',
            toolbar:{
                show: true,
            },
            name:"userNameList",
            data:defineUserArr
        })


        layer.open({
            type: 1,
            shadeClose:false,
            title:"权限设置",
            skin: 'layui-layer-rim', //加上边框
            area: ['70%', '60%'], //宽高
            content:layui.$("#addDiv"),
            end:function (){
                clearAddDiv();
            }
        });
    }









    function deleteByAuthId(obj){
        console.info(obj);
        $.ajax({
            url:"/webroot/decision/vcs/auth/delete/"+obj.data.id,
            type:"get",
            async:false,
            contentType:"application/json",
            success:function(userRet){
                if(userRet.errorCode == 200){
                    layer.msg('设置成功', {icon: 1});
                    query();
                }else{
                    layer.msg('删除失败', {icon: 5});
                }
            },
            error:function (){
                layer.msg('删除失败', {icon: 5});
            }
        });

    }





    layui.use('form', function(){
        var form = layui.form;
        form.on('submit(updateSubmit)', function(data){
            var flag = false
            $.ajax({
                url:"/webroot/decision/vcs/auth/set",
                type:"post",
                async:false,
                dataType:"json",
                data:JSON.stringify(data.field),
                contentType:"application/json",
                success:function(userRet){
                    if(userRet.errorCode == 500){
                        layer.msg(userRet.errorMsg, {icon: 5});
                    }else{
                        layer.closeAll();
                        layer.msg('更新成功', {icon: 1});
                        query();
                    }
                }
            })
            return false;
        });

        form.on('submit(addSubmit)', function(data){
            console.info(data.field);
            var flag = false
            $.ajax({
                url:"/webroot/decision/vcs/auth/set",
                type:"post",
                async:false,
                dataType:"json",
                data:JSON.stringify(data.field),
                contentType:"application/json",
                success:function(userRet){
                    if(userRet.errorCode == 500){
                        layer.msg(userRet.errorMsg, {icon: 5});
                    }else{
                        layer.closeAll();
                        layer.msg('新增成功', {icon: 1});
                        query();
                    }
                }
            })
            return false;
        });

    });




    function clearUpdateDiv(){
        $("#updateDiv").css("display","none");
        $("#updateFileName").val("");
        $("#updateShowFileName").val("");
        $("#updateIpLimit").val("");
        $("#updateUserLimit").empty();
    }


    function clearAddDiv(){
        $("#addDiv").css("display","none");
        $("#addIpLimit").val("");
        $("#addUserLimit").empty();
        $("#addFileList").empty();
        $("#addFileList").append("<option value=''>请选择</option>")
    }






</script>


<div id="updateDiv" style="display: none">
    <form class="layui-form" action="" lay-filter="updateForm" id="updateForm" style="margin-top: 20px;margin-right: 30px">
        <input type="text" id="updateFileName" class="layui-input" value="" style="display: none" name="fileName">

        <div class="layui-form-item">
            <label class="layui-form-label">文件名</label>
            <div class="layui-input-block">
                <input type="text"  lay-verify="title" autocomplete="off" id="updateShowFileName"  class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户列表</label>
                <div class="layui-input-inline" id="updateUserLimit"  name="userNameList" style="width: 400px">
                </div>
            </div>
        </div>



        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">IP限制</label>
            <div class="layui-input-block">
                <textarea  class="layui-textarea" id="updateIpLimit" name="ipList" ></textarea>
                <span style="color:#ff000087">例子:192.168.1.1 &nbsp;&nbsp;&nbsp;192.168.1.* &nbsp;&nbsp;&nbsp; 192.168.1-100。&nbsp;多个IP配置之间用英文逗号,</span>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="updateSubmit">立即提交</button>
            </div>
        </div>

    </form>
</div>





<div id="addDiv" style="display: none">
    <form class="layui-form" action="" lay-filter="addForm" id="addForm" style="margin-top: 20px;margin-right: 30px">


        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">文件列表</label>
                <div class="layui-input-inline">
                    <select id="addFileList" name="fileName">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户列表</label>
                <div class="layui-input-inline">
                    <div class="layui-input-inline" id="addUserLimit" style="width: 400px"></div>
                </div>
            </div>
        </div>



        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">IP限制</label>
            <div class="layui-input-block">
                <textarea  class="layui-textarea" id="addIpLimit" name="ipList" ></textarea>
                <span style="color:#ff000087">例子:192.168.1.1 &nbsp;&nbsp;&nbsp;192.168.1.* &nbsp;&nbsp;&nbsp; 192.168.1-100。&nbsp;多个IP配置之间用英文逗号,</span>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addSubmit">立即提交</button>
            </div>
        </div>

    </form>
</div>



</html>
